package com.irdstudio.apicenter.gateway.web.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.irdstudio.apicenter.gateway.comm.MaccsChannelClient;
import com.irdstudio.apicenter.gateway.comm.MaccsChannelFactory;
import com.irdstudio.apicenter.gateway.comm.MaccsPacket;
import com.irdstudio.apicenter.gateway.comm.MaccsPacketUtil;
import com.irdstudio.apicenter.gateway.web.MaccsException;
import com.irdstudio.apicenter.gateway.web.MaccsTradeDefineVo;
import com.irdstudio.apicenter.gateway.web.view.JsonView;
import com.irdstudio.apicenter.gateway.web.view.View;
/**
 * 
 * Maccs 交易转发控制
 * 	   根据交易定义及交易要素定义及转发渠道定义，将相应的交易转发到指定的渠道
 * @author guangming.li
 * @version 1.0
 * @date 2015-05-25
 */

@Controller
public class TradeForward implements IController {
	
	/* 文本日志对象 */
	private final static Logger logger = LoggerFactory
			.getLogger(TradeForward.class);
	
	@SuppressWarnings("unchecked")
	public View execute(MaccsTradeDefineVo tradeDefine, MaccsPacket inPacket,
			HttpServletRequest request, HttpServletResponse response) throws MaccsException {
		
		// 交易安全检查及必要参数检查
		String did = inPacket.getValue("d");
		String encode_msg = inPacket.getValue("e");
		String channel = inPacket.getValue("channel");		

		// 如果存在档板,则直接使用档板的数据，不与渠道进行交互
		if (tradeDefine.isUseBaffle()) {
			
			ObjectMapper mapper = new ObjectMapper();
			
			//允许出现特殊字符和转义符
			mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) ;
			
			//允许出现单引号
			mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true) ;
			
			MaccsPacket backPacket = null;
			try {
				backPacket = new MaccsPacket(mapper.readValue(
						tradeDefine.getTradeBaffleData(), Map.class));
			} catch (JsonParseException e) {
				e.printStackTrace();
				backPacket = MaccsPacketUtil.makeFailurePacket(e.getMessage());
			} catch (JsonMappingException e) {
				e.printStackTrace();
				backPacket = MaccsPacketUtil.makeFailurePacket(e.getMessage());
			} catch (IOException e) {
				e.printStackTrace();
				backPacket = MaccsPacketUtil.makeFailurePacket(e.getMessage());
			}
			
			return new JsonView(request, response, backPacket);
			
		} else {
			
			MaccsChannelClient client = MaccsChannelFactory
					.getChannelClient(tradeDefine.getForwardChannelId());

			if (client == null) {
				throw new MaccsException("获取转发渠道:"
						+ tradeDefine.getForwardChannelId() + "失败!");
			}
			
			logger.info("交易转发到渠道：" + tradeDefine.getForwardChannelId());

			// 通过转发渠道发送交易
			MaccsPacket backPacket = client.sendAndWait(tradeDefine, inPacket);
			return new JsonView(request, response, backPacket);
		}
	}
}
